<template>
	<view class="cf-box">  
		<view v-if="list.length>0" v-for="(item, index) in list" :key="index" @tap="toDetail(item)" class="cf-item-shu-box" style="width: 45%;">
			
			<view  >
				<view >
					标题：{{item.title}}
				</view>
				<view  class="cf-addDot">
					内容：{{item.ct}}
				</view>
				
				
			</view>
		</view>
		<view v-else>
			暂无模板
		</view>
		
		<ayAd :type="2" :showOffAt="true"></ayAd>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import storage from '@/store/storage.js'
	import ayAd from '@/components/ay-ad/ay-ad.vue';
	import cloud from '@/api/cloud.js'
	import comm from '@/api/comm.js'
	import preDupliClick from '@/api/preDupliClick.js'
	import timeConvert from '@/api/timeConvert.js'
	import ayswitchR from '../components/ay-switch/ay-switch-round.vue'
	import ayPopTips from "@/components/ay-pop/ay-pop-tips.vue"

	import ayslideLeft from '@/components/ay-operate/del_slideLeft.vue';

	export default {
		components: {
			ayAd,
			
		},
		watch: {

		},
		computed: {
			...mapState(['hasLogin'])
		},
		data() {
			return {
				
				list: [],
			};
		},
		onLoad: function(options) {
			
			let that = this;

			that.setData_init();
		},
		onShow: function() {
			let that = this;

		},
		methods: {
			toDetail(item){
				let that = this;
				let id = item.id ;
				console.log(id)
				uni.navigateTo({
					url: `/pagesScan/notify/notify?id_temp=` + id,
				})
				
			},
			setData_init() {
				let that = this;
				if (that.hasLogin) {
					that.userInfo = storage.getUserInfo();
					let userInfo = that.userInfo;
					that.getByYun(userInfo.openId);
				}else{
					comm.toLoginPage();
				}
				
			},
			async getByYun(id) {
				let that = this;
				let list = [] ;
				let whereSql = {
					id_user : id,					
					is_temp : 1 ,
				};
				const res_notify_temp = await that.getToYun(cloud.tb_name.notify_temp, whereSql);
				list = res_notify_temp ;
				
				whereSql = {
					is_temp_public : 1 ,
				};
				const res_notify_temp_2 = await that.getToYun(cloud.tb_name.notify_temp, whereSql);
				
				list = list.concat(res_notify_temp_2) ;
				that.list = list ;
			},
			
			addToYun(colName, data_add) {
				let that = this;
				let data = {
					colName: colName,
					data: data_add,
				};
				uniCloud.callFunction({
					name: 'add',
					data: data,
				})
			},

			async getToYun(colName, whereSql, fieldSql) {
				let that = this;
				let data = {
					colName: colName,
				};
				let str_whereSql = JSON.stringify(whereSql)
				let isWhereSql = (str_whereSql == "{}") || (str_whereSql == 'undefined');
				if (!isWhereSql) {
					data['where'] = whereSql;
				}

				let str_fieldSql = JSON.stringify(fieldSql)
				let isFieldSql = (str_fieldSql == "{}") || (str_fieldSql == 'undefined');
				if (!isFieldSql) {
					data['field'] = fieldSql;
				}

				// uniCloud.callFunction({
				// 	name: 'get',
				// 	data: data,
				// }).then((res) => {
				// 	that.list = res.result.data;
				// })
				const res = await that.$dateByCloud({
					name: 'get',
					data: data,
				})
				return res.result.data;
			},
			updateToYun(colName, data_update, _id) {
				let that = this;
				let data = {
					colName: colName,
					data: data_update,
					_id: _id,
				};
				uniCloud.callFunction({
					name: 'update',
					data: data,
				})
			},
			removeToYun(colName, id) {
				let that = this;
				let data = {
					colName: colName,
					id: id,
				};
				uniCloud.callFunction({
					name: 'remove',
					data: data,
				}).then((res) => {
					that.getToYun(colName); //刷新页面
				})
			},
			removeallToYun(colName) {
				let that = this;
				let data = {
					colName: colName,
				};
				uniCloud.callFunction({
					name: 'removeall',
					data: data,
				})
			},

		},
	}
</script>

<style lang="scss">
	

	.box-ct {
		margin-bottom: 20upx;
	}
	
</style>
